<?php
namespace Activity\Models;

class LotteryLog extends Base
{
    public function initialize()
    {

    }

    public function getSource()
    {
        return 'daishu_site_lottery_log';
    }

    public function gets()
    {
        $query = 'SELECT `uid`,`phone` FROM `daishu_site_lottery_log`';
        try {
            $ret = $this->db()->query($query)->fetchAll();
            if ($ret !== false && is_array($ret) && count($ret) > 0) {
                return $ret;
            }
        } catch (\PDOException $ex) {
            $this->errorLog('SELECT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**
     * @param array $entity
     * @param       $prize
     *
     * @return bool
     * @throws \Exception
     */
    public function adds(array $entity, $prize)
    {
        if (count($entity) == 0) {
            throw new \Exception('参数错误');
        }
        $keys = array_keys($entity[0]);
        $keys = array_map(function ($key)
        {
            return "`{$key}`";
        }, $keys);
        $keys = implode(',', $keys);
        $sql  = "INSERT INTO " . self::getSource() . " ({$keys}) VALUES ";
        foreach ($entity as $item) {
            $item   = array_map(function ($value)
            {
                return " '{$value}' ";
            }, $item);
            $values = implode(',', array_values($item));
            $sql    .= " ({$values}), ";
        }
        $sql = rtrim(trim($sql), ',');
        $cnt = count($entity);
        try {
            $this->db()->begin();
            if ($this->db()->execute($sql) == false) {
                $this->db()->rollback();
                return false;
            };
            $update = "UPDATE `daishu_site_lottery_prize` SET `number`=`number`-{$cnt} WHERE `id`=" . intval($prize);
            if ($this->db()->execute($update) == false) {
                $this->db()->rollback();
                return false;
            }
            $this->db()->commit();
            return true;
        } catch (\PDOException $ex) {
            $this->errorLog('INSERT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**
     * 单条添加
     *
     * @param $entity
     * @param $prize
     *
     * @return bool
     */
    public function add($entity, $prize)
    {
        try {
            $this->db()->begin();
            if ($this->db()->insertAsDict(self::getSource(), $entity) == false) {
                $this->db()->rollback();
                return false;
            }
            $update = "UPDATE `daishu_site_lottery_prize` SET `number`=`number`-1 WHERE `id`=" . intval($prize);
            if ($this->db()->execute($update) == false) {
                $this->db()->rollback();
                return false;
            }
            $this->db()->commit();
            return true;
        } catch (\PDOException $ex) {
            $this->errorLog('INSERT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    public function getList()
    {
        try {
            $query = $this->select('*')->from(self::class)->orderBy('id DESC');
            $ret   = $query->getQuery()->execute()->toArray();
            if ($ret !== false && is_array($ret) && count($ret) > 0) {
                return $ret;
            }
        } catch (\PDOException $ex) {
            $this->errorLog('SELECT', $query->getPhql(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }
}
